package com.example.sekerim;


import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.database.DatabaseHandler;
import com.example.database.WeightStorage;

public class RegistrationActivityPersonalInformation extends Activity {

	private TextView mTitleTextView;
	private TextView mHeightTextView;
	private TextView mHeightTextUnitView;
	private EditText mHeightEditText;
	private TextView mWeightTextView;
	private TextView mWeightUnitTextView;
	private EditText mWeightEditText;
	private TextView mFamilyHistoryCheckTextView;
	private RadioButton mFamilyHistorySickExist;
	private RadioButton mFamilyHistorySickNotExist;
	private TextView mSmokingCheckTextView;
	private RadioButton mSmokingRadioButton;
	private RadioButton mNotSmokingRadioButton;
	private TextView mEthnicityCheckTextView;
	private Spinner mEthnicityCheckSpinner;
	private Button mCompletionButton;

	private Patient patient = new Patient();
	private String ethnicity = "";

	private boolean isSickExistInFamilyHistory = false;
	private boolean isSmoking = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registration_personal_information);

		Bundle extras = getIntent().getExtras();
		patient.setBirthdayDay(extras.getInt("mDay"));
		patient.setBirthdayMonth(extras.getInt("mMonth"));
		patient.setBirthdayYear(extras.getInt("mYear"));
		patient.setUserName(extras.getString("userName"));
		patient.setUserSurname(extras.getString("userSurname"));
		patient.setPassword(extras.getString("password"));
		patient.setMailAddress(extras.getString("mailAddress"));
		patient.setGender(extras.getBoolean("isMale"));
		initializePageElements();


	}

	@SuppressLint("InlinedApi") public void initializePageElements()
	{
		mTitleTextView = (TextView) findViewById(R.id.registration_diabetics_info_page_title);
		mHeightTextView = (TextView) findViewById(R.id.registration_height_field);
		mHeightTextUnitView = (TextView) findViewById(R.id.registration_height_unit);
		mHeightEditText = (EditText) findViewById(R.id.registration_height_value);
		mWeightTextView= (TextView) findViewById(R.id.registration_weight_field);
		mWeightUnitTextView = (TextView) findViewById(R.id.registration_weight_unit);
		mWeightEditText = (EditText) findViewById(R.id.registration_weight_value);
		mFamilyHistoryCheckTextView = (TextView) findViewById(R.id.registration_family_history_check);
		mFamilyHistorySickExist = (RadioButton) findViewById(R.id.registration_radio_family_history_exist);
		mFamilyHistorySickNotExist = (RadioButton) findViewById(R.id.registration_radio_family_history_not_exist);
		mSmokingCheckTextView = (TextView) findViewById(R.id.registration_smoking_check);
		mSmokingRadioButton = (RadioButton) findViewById(R.id.registration_radio_smoking);
		mNotSmokingRadioButton = (RadioButton) findViewById(R.id.registration_radio_not_smoking);
		mEthnicityCheckTextView = (TextView) findViewById(R.id.registration_ethnicity_check_text);
		mEthnicityCheckSpinner = (Spinner) findViewById(R.id.registration_ethnicity_check_spinner);
		mCompletionButton = (Button) findViewById(R.id.registration_completion_button);

		mTitleTextView.setText(Constants.registration_diabetics_info_title);
		mHeightTextView.setText(Constants.registration_height_field);
		mHeightTextUnitView.setText(Constants.registration_height_unit);
		mWeightTextView.setText(Constants.registration_weight_field);
		mWeightUnitTextView.setText(Constants.registration_weight_unit);
		mFamilyHistoryCheckTextView.setText(Constants.registration_family_history_check);
		mFamilyHistorySickExist.setText(Constants.registration_family_history_sick_exist);
		mFamilyHistorySickNotExist.setText(Constants.registration_family_history_sick_not_exist);
		mSmokingCheckTextView.setText(Constants.registration_smoking_check_text);
		mSmokingRadioButton.setText(Constants.registration_smoking_radio_Button);
		mNotSmokingRadioButton.setText(Constants.registration_not_smoking_radio_Button);
		mEthnicityCheckTextView.setText(Constants.registration_ethnicity_check_text);
		mCompletionButton.setText(Constants.registration_completion_button);

		mFamilyHistorySickExist.setChecked(true);
		mFamilyHistorySickNotExist.setChecked(false);

		mFamilyHistorySickExist.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				isSickExistInFamilyHistory = true;
				mFamilyHistorySickExist.setChecked(true);
				mFamilyHistorySickNotExist.setChecked(false);

			}
		});
		mFamilyHistorySickNotExist.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				isSickExistInFamilyHistory = false;
				mFamilyHistorySickExist.setChecked(false);
				mFamilyHistorySickNotExist.setChecked(true);
			}
		});

		mSmokingRadioButton.setChecked(false);
		mNotSmokingRadioButton.setChecked(true);

		mSmokingRadioButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				isSmoking = true;
				mSmokingRadioButton.setChecked(true);
				mNotSmokingRadioButton.setChecked(false);

			}
		});
		mNotSmokingRadioButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				isSmoking = false;
				mSmokingRadioButton.setChecked(false);
				mNotSmokingRadioButton.setChecked(true);
			}
		});

		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, Constants.registration_ethnicity_list);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mEthnicityCheckSpinner.setAdapter(dataAdapter);
		mEthnicityCheckSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
				ethnicity = Constants.registration_ethnicity_list.get(pos);
				Toast.makeText(parent.getContext(), 
						"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				ethnicity = Constants.registration_ethnicity_list.get(0);

			}
		});

		Typeface font = Typeface.createFromAsset(getAssets(), "fonts/beryliumbold.ttf");
		mTitleTextView.setTypeface(font);


		mCompletionButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {

				mHeightEditText.setError(null);
				mWeightEditText.setError(null);

				// Store values at the time of the login attempt.
				String height = mHeightEditText.getText().toString();
				String weight = mWeightEditText.getText().toString();

				boolean cancel = false;
				View focusView = null;


				if (TextUtils.isEmpty(height)) {
					mHeightEditText.setError(Constants.registration_empty_height_text);
					focusView = mHeightEditText;
					cancel = true;
				}
				else if (TextUtils.isEmpty(weight)) {
					mWeightEditText.setError(Constants.registration_empty_weight_text);
					focusView = mWeightEditText;
					cancel = true;
				}

				if (cancel) {
					focusView.requestFocus();
				} else {
					patient.setEthnicity(ethnicity);
					patient.setFamilyHistoryClean(!isSickExistInFamilyHistory);
					patient.setIsSmoking(isSmoking);
					patient.setHeight(Integer.parseInt(mHeightEditText.getText().toString()));
					patient.setWeight(Integer.parseInt(mWeightEditText.getText().toString()));

					try {
						InternalStorage.writeObject(getApplicationContext(), Constants.shared_info, patient);
					} catch (IOException e) {
						Log.e("While writing: ", e.getMessage());
					}

					DatabaseHandler db = new DatabaseHandler(RegistrationActivityPersonalInformation.this);
					Log.d("Insert: ", "Inserting .."); 
					DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
					//get current date time with Date()
					Date date = new Date();
					db.addWeight(new WeightStorage(patient.getWeight(), dateFormat.format(date)));
					Constants.homepage_left_menu[0] = patient.getUserName() + " " + patient.getUserSurname();

					SaveSharedPreference.setMailAddress(RegistrationActivityPersonalInformation.this, patient.getMailAddress());

					Intent profileIntent = new Intent(RegistrationActivityPersonalInformation.this,HomePageActivity.class);
					profileIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
					profileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
					startActivity(profileIntent);
					finish();
				}

			}
		});

	}

}